//
//  WJTabBar.m
//  weibo
//
//  Created by 王俊 on 15/12/20.
//  Copyright © 2015年 王俊. All rights reserved.
//

#import "WJTabBar.h"

@interface WJTabBar()
@property(nonatomic,strong) UIButton  *plusBtn;

@end

@implementation WJTabBar

# pragma mark - +号按键的懒加载

- (UIButton*)plusBtn
{
    if (_plusBtn == nil) {
        _plusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button"] forState:UIControlStateNormal];
        [_plusBtn setBackgroundImage:[UIImage imageNamed:@"tabbar_compose_button_highlighted"] forState:UIControlStateHighlighted];
        [_plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add"] forState:UIControlStateNormal];
        [_plusBtn setImage:[UIImage imageNamed:@"tabbar_compose_icon_add_highlighted"] forState:UIControlStateHighlighted];
        [self addSubview:_plusBtn];
        [_plusBtn addTarget:self action:@selector(plusBtnClicked:) forControlEvents:UIControlEventTouchUpInside];

    }
        return _plusBtn;
}


- (void)plusBtnClicked:(UIButton*)btn
{
    if ([self.delegate respondsToSelector:@selector(tabBar:selectedPlusButton:)]) {
        [self.delegate tabBar:self selectedPlusButton:btn];
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    int btnX = 0;
    int btnY = 0;
    int i = 0;
    CGFloat width = self.frame.size.width / (self.items.count + 1);
    CGFloat height = 50;
    //NSLog(@"%@",self.subviews);
    //调整系统自带tabBar上的按钮位置
    for (UIView *tabBarButton in self.subviews) {
        if ([tabBarButton isKindOfClass:NSClassFromString(@"UITabBarButton")]) {
            if (i == 2) {
                i++;
            }
            tabBarButton.frame = CGRectMake(btnX + i*width, btnY, width, height);
            i++;
        }
    }
    self.plusBtn.center = CGPointMake(self.bounds.size.width/2, self.bounds.size.height/2);
    self.plusBtn.bounds = CGRectMake(0, 0, _plusBtn.currentBackgroundImage.size.width, _plusBtn.currentBackgroundImage.size.height);
    
}
@end
